# Copyright 2024 The DLRover Authors. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("//:workspace.bzl", "xpu_cc_binary", "xpu_cc_library")
load("@xpu_timer_cfg//:xpu_config.bzl", "XPU_TIMER_CONFIG")

package(default_visibility = ["//visibility:public"])

genrule(
    name = "generate_version",
    srcs = ["generate_version.sh"],
    outs = ["version.h"],
    cmd = "$(location generate_version.sh) $(@D)/version.h",
    stamp = 1,
)

xpu_cc_library(
    name = "platform",
    srcs = [
        "platform.cc",
    ],
    hdrs = [
        "platform.h",
    ],
    deps = [
        ":logging",
        ":util",
    ],
)

cc_library(
    name = "signal_handler",
    srcs = [
        "signal_handler.cc",
    ],
    hdrs = [
        "signal_handler.h",
    ],
    deps = [
        ":logging",
        ":util",
        "//xpu_timer/protos:cc_hosting_service_proto",
        "//xpu_timer/server",
        "@org_gnu_libunwind//:unwind",
    ],
)

cc_library(
    name = "macro",
    hdrs = [
        "macro.h",
    ],
)

cc_library(
    name = "logging",
    srcs = [
        "logging.cc",
    ],
    hdrs = [
        "logging.h",
    ],
    deps = [
        ":util",
    ],
)

xpu_cc_library(
    name = "util",
    srcs = [
        "util.cc",
    ],
    hdrs = [
        "constant.h",
        "util.h",
        ":generate_version",
    ],
    deps_on_platform = False,
    deps = [
        ":macro",
        "@apache_brpc//:butil",
        "@boost//:interprocess",
        "@boost//:process",
        "@boost//:property_tree",
        "@openssl//:crypto",
        "@openssl//:ssl",
    ],
)

cc_library(
    name = "xpu_timer",
    srcs = [
        "xpu_timer.cc",
    ],
    hdrs = [
        "xpu_timer.h",
    ],
    deps = [
        ":logging",
    ],
)

#xpu_cc_binary(
#    name = "metrics_test",
#    srcs = ["metrics_test.cc"],
#    deps = [
#        ":metrics",
#        "//xpu_timer/nvidia:nvidia_timer",
#        "//xpu_timer/protos:cc_hosting_service_proto",
#        "@apache_brpc//:bvar",
#    ],
#)

cc_library(
    name = "py_stack_util",
    srcs = [
        "stack_util.cc",
    ],
    hdrs = [
        "stack_util.h",
    ],
    deps = [
        ":macro",
        ":util",
        ":xpu_timer",
        "//xpu_timer/protos:cc_hosting_service_proto",
    ],
)

xpu_cc_library(
    name = "bvar_prometheus",
    srcs = [
        "bvar_prometheus.cc",
    ],
    hdrs = [
        "bvar_prometheus.h",
    ],
    deps = [
        ":logging",
        ":metrics",
        ":util",
        ":xpu_timer",
        "//xpu_timer/server:server",
        "@apache_brpc//:bvar",
        "@com_github_jupp0r_prometheus_cpp//pull",
    ],
)

xpu_cc_library(
    name = "metrics",
    srcs = [
        "metrics.cc",
    ],
    hdrs = [
        "constant.h",
        "metrics.h",
        "xpu_timer.h",
    ],
    deps = [
        ":util",
        ":xpu_timer",
        "//xpu_timer/protos:cc_hosting_service_proto",
        "@apache_brpc//:bvar",
    ],
)

xpu_cc_library(
    name = "manager",
    srcs = [
        "manager.cc",
    ],
    hdrs = [
        "manager.h",
    ],
    deps = [
        ":bvar_prometheus",
        ":logging",
        ":macro",
        ":metrics",
        ":py_stack_util",
        ":signal_handler",
        ":util",
        "//xpu_timer/protos:cc_hook_proto",
        "//xpu_timer/protos:cc_hosting_service_proto",
        "//xpu_timer/python:tracing_lib_loader",
        "//xpu_timer/server",
        "@apache_brpc//:brpc",
        "@boost//:interprocess",
    ] + XPU_TIMER_CONFIG.timer_deps,
    alwayslink = 1,
)
